# -*- coding: utf-8 -*-
import subprocess
import sys
import os
import codecs

def install_requirements():
    try:
        print("开始安装依赖...")
        
        # 先安装 PyQt6 及其依赖
        pyqt_packages = [
            'PyQt6==6.5.3',
            'PyQt6-Qt6==6.5.3',
            'PyQt6-sip==13.9.0'
        ]
        
        for package in pyqt_packages:
            print(f"安装 {package}...")
            try:
                subprocess.check_call([
                    sys.executable,
                    '-m',
                    'pip',
                    'install',
                    package,
                    '--no-cache-dir',
                    '--only-binary=:all:',
                    '-i',
                    'https://pypi.tuna.tsinghua.edu.cn/simple',
                    '--trusted-host',
                    'pypi.tuna.tsinghua.edu.cn'
                ])
            except Exception as e:
                print(f"警告: 安装 {package} 失败: {str(e)}")
                sys.exit(1)
        
        # 安装其他基础依赖
        base_packages = [
            'numpy',
            'Pillow',
            'opencv-python-headless',
            'onnxruntime',  # rembg 的依赖
            'torch',        # rembg 的依赖
            'torchvision'   # rembg 的依赖
        ]
        
        for package in base_packages:
            print(f"安装 {package}...")
            try:
                subprocess.check_call([
                    sys.executable,
                    '-m',
                    'pip',
                    'install',
                    package,
                    '--only-binary=:all:',
                    '--no-cache-dir',
                    '-i',
                    'https://pypi.tuna.tsinghua.edu.cn/simple',
                    '--trusted-host',
                    'pypi.tuna.tsinghua.edu.cn'
                ])
            except Exception as e:
                print(f"警告: 安装 {package} 失败: {str(e)}")
        
        # 最后安装 rembg
        print("安装 rembg...")
        subprocess.check_call([
            sys.executable,
            '-m',
            'pip',
            'install',
            'rembg',
            '--no-cache-dir',
            '-i',
            'https://pypi.tuna.tsinghua.edu.cn/simple',
            '--trusted-host',
            'pypi.tuna.tsinghua.edu.cn'
        ])
        
        print("依赖安装完成")
        
    except Exception as e:
        print(f'发生错误: {str(e)}')
        sys.exit(1)

if __name__ == '__main__':
    install_requirements() 